<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('类型名称')">
              <a-input :placeholder="$t('请输入类型名称')" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('类型编码')">
              <a-input :placeholder="$t('请输入类型编码')" v-model="queryParam.code"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('管理状态')">
                <j-switch :placeholder="$t('请选择管理状态')" v-model="queryParam.manageState" query></j-switch>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-has="'memberRule:add'" @click="handleAdd" type="primary" icon="plus" v-if="(isChain && !tenantid) || !isChain">{{
        $t('新增')
      }}</a-button>
      <a-button v-has="'memberRule:export'" type="primary" icon="download" :loading="exloading" @click="handleExportXls($t('会员类型管理'))">{{
        $t('导出')
      }}</a-button>
      <span v-if="isChain && tenantid != ''" style="color:orange">{{$t('您的系统为连锁系统，会员类型需要在总部进行统一管理，请登录总部进行添加修改。')}}</span>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSources"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">{{
            $t('下载')
          }}</a-button>
        </template>
        <span slot="forApplet" slot-scope="text, record">
          <a-switch
            v-model="record.isForApplet"
            :disabled="!((isChain && !tenantid) || !isChain)"
            @change="onChange(record)"
          ></a-switch>
        </span>
        <span slot="action" slot-scope="text, record">
          <!-- <a @click="handleLottery(record)">{{ $t('彩票规则') }}</a>
          <a-divider type="vertical" />
          <a @click="handleEquity(record)">{{ $t('权益') }}</a>
          <a-divider type="vertical" /> -->
          <a v-has="'memberRule:edit'" @click="handleSet(record)">{{ $t('会员规则') }}</a>
          <a-divider v-has="'memberRule:edit'" type="vertical" />
          <a v-has="'memberRule:edit'" v-if="record.name != $t('非会员') && ((isChain && !tenantid) || !isChain)" @click="handleEdit(record)">{{
            $t('编辑')
          }}</a>
          <a-divider v-has="'memberRule:edit'" v-if="record.name != $t('非会员') && ((isChain && !tenantid) || !isChain)" type="vertical" />
          <a @click="handleDetail(record)">{{ $t('详情') }}</a>
          <a-divider v-has="'memberRule:delete'" type="vertical" v-if="(isChain && !tenantid) || !isChain" />
          <a-popconfirm :title="$t('您确定要删除该会员类型吗？')" @confirm="() => handleDelete(record.id)">
            <a v-has="'memberRule:delete'" v-if="(isChain && !tenantid) || !isChain">{{ $t('删除') }}</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <member-rule-modal ref="modalForm" @ok="modalFormOk"></member-rule-modal>
    <rule-set-modal ref="modalSet" @ok="modalFormOk"></rule-set-modal>
    <equity-modal ref="modalEquity"></equity-modal>
    <lottery-modal ref="modalLottery"></lottery-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import Vue from 'vue'
import { TENANT_ID } from '@/store/mutation-types'
import { putAction, getAction } from '@/api/manage'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import MemberRuleModal from './modules/MemberRuleModal'
import EquityModal from './modules/EquityModal'
import LotteryModal from './modules/LotteryModal'
import RuleSetModal from './modules/RuleSetModal'

export default {
  name: 'MemberRuleList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    MemberRuleModal,
    EquityModal,
    LotteryModal,
    RuleSetModal
  },
  data() {
    return {
      description: '会员类型管理管理页面',
      // 表头
      columns: [
        {
          title: this.$t('排序'),
          align: "center",
          sorter: true,
          dataIndex: 'sort'
        },
        {
          title: this.$t('类型名称'),
          align: 'center',
          dataIndex: 'name',
        },
        {
          title: this.$t('类型编码'),
          align: 'center',
          sorter: true,
          dataIndex: 'code',
        },
        {
          title: this.$t('首次取卡费'),
          align: 'center',
          dataIndex: 'firstFee',
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('再次取卡费'),
          align: 'center',
          dataIndex: 'againFee',
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('销售折扣'),
          align: 'center',
          dataIndex: 'saleDiscount',
          customRender: function (text, record) {
            if (!text) {
              return ''
            } else {
              return text + '%'
            }
          },
        },
        {
          title: this.$t('币兑换折扣'),
          align: 'center',
          dataIndex: 'exCoinDiscount',
          customRender: function (text, record) {
            if (!text) {
              return ''
            } else {
              return text + '%'
            }
          },
        },
        {
          title: this.$t('彩票兑换折扣'),
          align: 'center',
          dataIndex: 'exLotteryDiscount',
          customRender: function (text, record) {
            if (!text) {
              return ''
            } else {
              return text + '%'
            }
          },
        },
        {
          title: this.$t('A票兑换折扣'),
          align: 'center',
          dataIndex: 'exAlotteryDiscount',
          customRender: function (text, record) {
            if (!text) {
              return ''
            } else {
              return text + '%'
            }
          },
        },
        {
          title: this.$t('积分兑换折扣'),
          align: 'center',
          dataIndex: 'exIntegralDiscount',
          customRender: function (text, record) {
            if (!text) {
              return ''
            } else {
              return text + '%'
            }
          },
        },
        {
          title: this.$t('A币兑换折扣'),
          align: 'center',
          dataIndex: 'exPointsDiscount',
          customRender: function (text, record) {
            if (!text) {
              return ''
            } else {
              return text + '%'
            }
          },
        },
        {
          title: this.$t('晋级成长值'),
          align: 'center',
          dataIndex: 'promoteGrowth',
          customRender: (text, record) => {
            if (!this.autoPromote) {
              return '--'
            }
            return text
          },
        },
        {
          title: this.$t('管理状态'),
          align: 'center',
          dataIndex: 'manageState',
          customRender: (text) => (text ? filterMultiDictText(this.dictOptions['manageState'], text) : ''),
        },
        {
          title: this.$t('小程序会员专用'),
          align: 'center',
          dataIndex: 'forApplet',
          scopedSlots: { customRender: 'forApplet' },
        },
        {
          title: this.$t('说明'),
          align: 'center',
          dataIndex: 'commet',
        },
        {
          title: this.$t('操作'),
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/member/bisMemberRule/list',
        delete: '/member/bisMemberRule/delete',
        deleteBatch: '/member/bisMemberRule/deleteBatch',
        exportXlsUrl: '/member/bisMemberRule/exportXls',
        importExcelUrl: 'member/bisMemberRule/importExcel',
      },
      queryParam: {
        notNonmember: 1,
      },
      dictOptions: {},
      superFieldList: [],
      dataSources: [],
      autoPromote: false,
      tenantid: Vue.ls.get(TENANT_ID),
      isChain: JSON.parse(localStorage.getItem('isChain')),
    }
  },
  watch: {
    dataSource(val) {
      val.forEach((element) => {
        element.isForApplet = element.forApplet == 0 ? false : true
      })
      this.dataSources = val
    },
  },
  created() {
    this.$set(this.dictOptions, 'manageState', [
      { text: this.$t('是'), value: 'Y' },
      { text: this.$t('否'), value: 'N' },
    ])
    this.getSuperFieldList()
    this.queryByName('autoPromote')
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    searchReset() {
      ;(this.queryParam = {
        notNonmember: 1,
      }),
        this.loadData(1)
    },
    handleEdit: function (record) {
      this.$refs.modalForm.autoPromote = this.autoPromote
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = this.$t('编辑')
      this.$refs.modalForm.disableSubmit = false
    },
    handleAdd: function () {
      this.$refs.modalForm.autoPromote = this.autoPromote
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = this.$t('新增')
      this.$refs.modalForm.disableSubmit = false
    },
    queryByName(name) {
      getAction('/bis/bisSysParameter/queryByName', { name: name }).then((res) => {
        if (res.success) {
          if (res.result != null && res.result.ivalue == 1) {
            this.autoPromote = true
          }
        }
      })
    },
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'int', value: 'sort', text: this.$t('排序'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'name', text: this.$t('类型名称'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'code', text: this.$t('类型编码'), dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'firstFee', text: this.$t('首次取卡费'), dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'againFee', text: this.$t('再次取卡费'), dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'exCoinDiscount', text: this.$t('币兑换折扣'), dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'exLotteryDiscount', text: this.$t('彩票兑换折扣'), dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'exAlotteryDiscount', text: this.$t('A票兑换折扣'), dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'exIntegralDiscount', text: this.$t('积分兑换折扣'), dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'exPointsDiscount', text: this.$t('A币兑换折扣'), dictCode: '' })
      fieldList.push({ type: 'switch', value: 'manageState', text: this.$t('管理状态') })
      fieldList.push({ type: 'string', value: 'commet', text: this.$t('说明'), dictCode: '' })
      this.superFieldList = fieldList
    },
    onChange(r) {
      let that = this
      if (r.isForApplet) {
        putAction('/member/bisMemberRule/forApplet', {
          id: r.id,
          forApplet: r.isForApplet ? 1 : 0,
        }).then((res) => {
          if (res.success) {
            that.loadData()
          } else {
            that.$message.error(res.message)
          }
        })
      } else {
        that.$message.warn(this.$t('必须设置一个小程序会员注册使用的会员类型'))
      }
    },
    handleEquity(record) {
      if ((this.isChain && this.tenantid == '') || !this.isChain) {
        this.$refs.modalEquity.disableSubmit = false
      } else {
        this.$refs.modalEquity.disableSubmit = true
      }
      this.$refs.modalEquity.edit(record)
    },
    handleLottery(record) {
      this.$refs.modalLottery.edit(record)
    },
    handleSet(record) {
      if ((this.isChain && this.tenantid == '') || !this.isChain) {
        this.$refs.modalSet.disableSubmit = false
      } else {
        this.$refs.modalSet.disableSubmit = true
      }
      this.$refs.modalSet.edit(record)
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>